//
//  CargoChooseCell.m
//  KuWoCan
//
//  Created by hwd on 17/3/14.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "CargoChooseCell.h"

@interface CargoChooseCell ()<UITextFieldDelegate>

@property (nonatomic,strong) UIImageView *oStoreImgView;

@property (nonatomic,strong) UILabel *oStoreLab;

@property (nonatomic,strong) UILabel *oEtalonParaLab;

@property (nonatomic,strong) UILabel *oTotalParaLab;

@property (nonatomic,strong) UIButton *addBtn;

@property (nonatomic,strong) UIButton *subtractBtn;

@property (nonatomic,assign) NSInteger maxBoxCount;

@property (nonatomic,assign) NSInteger count;

@end


@implementation CargoChooseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    _oStoreImgView = [[UIImageView alloc] init];
    _oStoreImgView.image = [UIImage imageNamed:@"kuwo_logo"];
    [self addSubview:_oStoreImgView];
    [_oStoreImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20 * screenW_6));
        make.top.equalTo(@(18 * screenW_6));
        make.height.width.equalTo(@(65 * screenW_6));
    }];
    
    //parameter:商品名
    _oStoreLab = [UILabel new];
    _oStoreLab.textColor = [UIColor blackColor];
    _oStoreLab.font = KwFont(16 * screenW_6);
    _oStoreLab.text = @"Product Name";
    [self addSubview:_oStoreLab];
    [_oStoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_oStoreImgView);
        make.left.equalTo(_oStoreImgView.mas_right).offset(15 * screenW_6);
        make.right.equalTo(@0);
        make.height.equalTo(@(16 * screenW_6));
    }];
    
    
    UILabel *oEtalonConstLab = [UILabel new];
    oEtalonConstLab.textColor = [UIColor grayColor];
    oEtalonConstLab.font = [UIFont systemFontOfSize:13 * screenW_6];
    oEtalonConstLab.text = @"规格:";
    [self addSubview:oEtalonConstLab];
    [oEtalonConstLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_oStoreLab.mas_left);
        make.height.equalTo(@(13 * screenW_6));
        make.top.equalTo(_oStoreLab.mas_bottom).offset(10 * screenW_6);
    }];
    
    //parameter:规格
    _oEtalonParaLab = [UILabel new];
    _oEtalonParaLab.textColor = [UIColor grayColor];
    _oEtalonParaLab.font = [UIFont systemFontOfSize:13 * screenW_6];
    _oEtalonParaLab.text = @"100ML*12";
    [self addSubview:_oEtalonParaLab];
    [_oEtalonParaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(oEtalonConstLab.mas_right).offset(5 * screenW_6);
        make.height.equalTo(@(13 * screenW_6));
        make.top.equalTo(oEtalonConstLab.mas_top);
    }];
    
    //parameter:金额
    _oTotalParaLab = [UILabel new];
    _oTotalParaLab.textColor = [UIColor redColor];
    _oTotalParaLab.font = [UIFont systemFontOfSize:17 * screenW_6];
    _oTotalParaLab.textAlignment = NSTextAlignmentLeft;
    _oTotalParaLab.text = @"￥59";
    [self addSubview:_oTotalParaLab];
    [_oTotalParaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(oEtalonConstLab.mas_left);
        make.height.equalTo(@(17 * screenW_6));
        make.top.equalTo(oEtalonConstLab.mas_bottom).offset(10 * screenW_6);
    }];
    
    //加号 按钮
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _addBtn.backgroundColor = [UIColor redColor];
    [_addBtn setImage:kImg(@"tianjia") forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_addBtn];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-20 * screenW_6));
//        make.bottom.equalTo(_oStoreImgView.mas_bottom);
        make.top.equalTo(@(60 * screenW_6));
        make.size.mas_equalTo(CGSizeMake(25 * screenW_6, 25 * screenW_6));
    }];
    
    //parameter:数量
    _oNumberParaLab = [UITextField new];
    _oNumberParaLab.textColor = [UIColor blackColor];
    _oNumberParaLab.delegate = self;
    _oNumberParaLab.keyboardType = UIKeyboardTypeNumberPad;
    _oNumberParaLab.returnKeyType = UIReturnKeyDone;
    _oNumberParaLab.font = [UIFont systemFontOfSize:15 * screenW_6];
    _oNumberParaLab.textAlignment = NSTextAlignmentCenter;
    _oNumberParaLab.text = @"20";
//    _oNumberParaLab.adjustsFontSizeToFitWidth = YES;
    [self addSubview:_oNumberParaLab];
    [_oNumberParaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_addBtn.mas_left);
        make.height.equalTo(@(15 * screenW_6));
        make.width.greaterThanOrEqualTo(@(40 * screenW_6));
        make.centerY.equalTo(_addBtn.mas_centerY);
    }];
    
    //减号按钮
    _subtractBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _subtractBtn.backgroundColor = [UIColor blackColor];
    [_subtractBtn setImage:kImg(@"jianshao") forState:UIControlStateNormal];
    [_subtractBtn addTarget:self action:@selector(cutBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_subtractBtn];
    [_subtractBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_oNumberParaLab.mas_left);
        make.bottom.equalTo(_addBtn.mas_bottom);
        make.left.greaterThanOrEqualTo(_oTotalParaLab.mas_right).offset(10 * screenW_6);
        make.size.mas_equalTo(CGSizeMake(25 * screenW_6, 25 * screenW_6));
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = KwGaryColor;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(_oStoreImgView.mas_bottom).offset(15);
        make.height.equalTo(@1.5);
        make.bottom.equalTo(@0);
    }];
    
}

// 数量加按钮
- (void)addBtnClick
{
    if (_isUpdateInventory)
    {
        if (_maxBoxCount >= 0)
        {
            if (_count >= _maxBoxCount)
            {
                return;
            }
            else
            {
                _count += 10;
            }
        }
        else
        {
            _count += 10;
        }
    }
    else
    {
        _count += 10;
    }
    
    
    if (self.numAddBlock) {
        self.numAddBlock([NSString stringWithFormat:@"%ld",(long)_count]);
    }
}

// 数量减按钮
-(void)cutBtnClick
{
    _count -= 10;
    if (_count <= 0)
    {
        _count = 0;
    }

    if (self.numCutBlock) {
        self.numCutBlock([NSString stringWithFormat:@"%ld",(long)_count]);
    }
}

- (void)setModel:(ESSInventoryModel *)model
{
    _model = model;
    [_oStoreImgView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail] placeholderImage:kImg(kPlacehodelImg)];
    _oStoreLab.text = [NSString stringWithFormat:@"%@ %@",model.productName,model.tasteName];
    _oEtalonParaLab.text = model.specificationsName;
    _count =  model.totalBox? [model.totalBox integerValue] : 0;
    _oNumberParaLab.text = model.totalBox ? :@"0";
    if (_isUpdateInventory)
    {
        if (model.totalBox && [model.totalBox integerValue] > 0)
        {
            _maxBoxCount = [model.totalBox integerValue];
        }
    }
    
    _oTotalParaLab.text = [NSString stringWithFormat:@"¥%@",model.price];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.text.isEmptyString) {
        _count = 0;
        if (self.numAddBlock) {
            self.numAddBlock(@"0");
        }
        return YES;
    }
    _count = [textField.text integerValue];
    if (self.numAddBlock) {
        self.numAddBlock([NSString stringWithFormat:@"%ld",(long)_count]);
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text.isEmptyString) {
        _count = 0;
        if (self.numAddBlock) {
            self.numAddBlock(@"0");
        }
        return;
    }
    _count = [textField.text integerValue];
    if (self.numAddBlock) {
        self.numAddBlock([NSString stringWithFormat:@"%ld",(long)_count]);
    }
}

@end
